<%=packageName%>
<% import org.codehaus.groovy.grails.orm.hibernate.support.ClosureEventTriggeringInterceptor as Events %>

<%String domainName=propertyName.replace("Instance",""); 
%>
<body>

<!--Search Area  -->	  
  <div class="body" id="${domainName}_root">
	<fieldset>
			<legend>查询条件</legend>
			<form id="${domainName}_search">
				<table>
				<tr class="prop">
					<td valign="top" class="name" width="18%">
	                   <label for="name">Name:</label>
	                </td>
	                <td valign="top" width="32%">
	                   <input type="text" name="name_mask" id="item_name" onkeydown="doSearch(arguments[0]||event)" />
	                	<input type="checkbox" id="autosearch" onclick="enableAutosubmit(this.checked)">
						允许自动搜索 <br/>
	                </td>
				</tr>
				<tr class="prop">
					<td valign="top" class="name" width="18%">
	                   <label for="name">Name2:</label>
	                </td>
	                <td valign="top"  width="32%">
	                  <input type="text" id="item_name2" name="name_mask2" onkeydown="doSearch(arguments[0]||event)" />
	                </td>
				</tr>
				</table>
			<!--Button Area -->		
			<div id='${domainName}_buttons' class="buttons" align="right">
                <input  id="${domainName}_button_search" name="2" type="button" value="查询" class="formbutton" />
                <input type="reset" value="重置" class="formbutton" />
               	<input  id="${domainName}_button_create" type="button" value="新建" class="formbutton" name="1" />
               	<input  id="${domainName}_button_edit" type="button" value="修改" class="formbutton" name="3" />
                <input  id="${domainName}_button_view" type="button" value="详细" class="formbutton" name="5"/>
                <input  id="${domainName}_button_delete" type="button" value="删除" class="formbutton" name="4"/>
            </div>						
			<!--Button Area end-->					
	</form>
	</fieldset>
<!--Search Area end-->	

<!--List Area -->
           
     <div id="${domainName}_list">
		 <table id="${domainName}_list2" class="scroll" cellpadding="0" cellspacing="0"></table> 
		 <div id="${domainName}_pager2" class="scroll" style="text-align:center;"></div> 
	 </div>		
	
<!--List Area end -->	
	
<!--dialog Area -->
 <div id="${domainName}_dialog">
	<input type="hidden" id="${domainName}_id" value=""/>
	<input type="hidden" id="${domainName}_action" value=""/>
	<form id="${domainName}_dialog_form">
                  <table>
                        <tbody>
                        	<tr>
                        		<td colspan="4">
                        			<div class="error">
                        		</td>
                        	</tr>
                        <%
                            excludedProps = ['version',
                                             'id',
                                               Events.ONLOAD_EVENT,
                                               Events.BEFORE_DELETE_EVENT,
                                               Events.BEFORE_INSERT_EVENT,
                                               Events.BEFORE_UPDATE_EVENT]
                            props = domainClass.properties.findAll { !excludedProps.contains(it.name) }

                            Collections.sort(props, comparator.constructors[0].newInstance([domainClass] as Object[]))
							int count = 0;
                            props.each { p ->
                                if(!Collection.class.isAssignableFrom(p.type)) {
                                    cp = domainClass.constrainedProperties[p.name]
                                    display = (cp ? cp.display : true)        
                                    if(display) {
										if(count % 2 == 0){
						%>
                            <tr class="prop">
                                <td valign="top" class="name" width="18%">
                                    <label for="${p.name}">${p.naturalName}:<%if(cp!=null && cp.blank == false) {%> *<%} %></label>
                                </td>
                                <td valign="top" class="value \${hasErrors(bean:${propertyName},field:'${p.name}','errors')}" width="32%">
                                    ${renderEditor(p)}
                                </td>
						<%				
										}else{
						%>
                                <td valign="top" class="name" width="18%">
                                    <label for="${p.name}">${p.naturalName}:</label>
                                </td>
                                <td valign="top" class="value \${hasErrors(bean:${propertyName},field:'${p.name}','errors')}" width="32%">
                                    ${renderEditor(p)}
                                </td>
                            </tr> 
                        <%		  		}
										count++;
									}
								}
							}
							if(count % 2 != 0){
						%>
                                <td colspan="2">&nbsp;
                                </td>
							</tr>
						<%
							}
						%>
                        </tbody>
                    </table>
              </form>
 </div>
	
<!--dialog Area end -->	


</div>

</body>

<!--js area -->		
	<script type="text/javascript"><!--
		jQuery(document).ready(function(){
			var	allFields=jQuery('#${domainName}_dialog_form input');
			allFields.val();
			//jQuery(allFields).each(function(){alert(jQuery(this).attr('id'));});
			var	tips = jQuery("#${domainName}_validateTips");
			jQuery("#${domainName}_dialog").dialog({
			title: '创建***',
			bgiframe: true,
			autoOpen: false,
			height: 300,
			width:600,
			position:'top',
			modal: true,
			buttons: {
					'Save': function() {
						var bValid = true;
						var params ;
						var id=jQuery('#${domainName}_id').val();
						var action=jQuery('#${domainName}_action').val();
						params= jQuery('#${domainName}_dialog_form').serialize()+'&id='+id;
						allFields.removeClass('ui-state-error');
						//validate
						//var validator = \$('#${domainName}_dialog_form').validate();
						//validator.showErrors({"${domainName}_dailog_msg": "ABCDE!"});
						
						
						if (bValid) {
							//ajax save
							if(action=='c'){
									jQuery.post('${domainName}/save',params,function(s){});
							}
							if(action=='u'){
								jQuery.post('${domainName}/update',params,function(s){});
							}
							if(action=='v')
								jQuery(this).dialog('close');
							jQuery(this).dialog('close');
							gridReload();
						}
					},
					'Cancel': function() {
						jQuery(this).dialog('close');
					}
				},
			close: function() {
					allFields.val('').removeClass('ui-state-error');
					allFields.val('').removeClass('error');
					\$("#${domainName}_dialog_form div.error").attr("style", function() { return "display: none;" }); 
				}
			});

			//start button click
			jQuery('#${domainName}_buttons :button').click(function(){
				var index=0;
				index=parseInt(jQuery(this).attr('name'));
				//validate
				\$('#${domainName}_dialog').ready(function(){
					\$('#${domainName}_dialog_form').validate({
						event:"blur",
						errorLabelContainer: \$("#${domainName}_dialog_form div.error")
						});
				});
				//user defined validate function
				userDefineValidate();
			
				switch(index)
				{
				//create
				case 1:
					jQuery('#${domainName}_id').val();
					jQuery('#${domainName}_action').val('c');
					jQuery('#${domainName}_dialog').dialog('option', 'title', '新建');
					jQuery('#${domainName}_dialog').dialog('open');
					
				break;
				//inquery
				case 2:
					gridReload();
				break;
				//update
				case 3:
					//jQuery('${domainName}_dailog_form #id').val(1);
					jQuery('#${domainName}_action').val('u');
					jQuery('#${domainName}_dialog').dialog('option', 'title', '编辑');
					doShow();
					jQuery('#${domainName}_dialog').dialog('open');
				break;
				//delete
				case 4: 
					if(confirm('确定删除数据?'))
					{
						
						jQuery.post('${domainName}/delete',{id:jQuery('#${domainName}_id').val()},function(s){
								
								if(s){
									alert('删除成功!');
									gridReload();	
									}
								else
									{alert('删除失败!');}
								gridReload();			
							});	
					}
				break;
				//view
				case 5:
					jQuery('#${domainName}_action').val('v');
					jQuery('#${domainName}_dialog').dialog('option', 'title', '查看');
					doShow();
					jQuery('#${domainName}_dialog').dialog('open');
					
				break;	
				default:
					alert('default');
				break;
				};
		    }).hover(
					function(){ 
						jQuery(this).addClass("ui-state-hover"); 
					},
					function(){ 
						jQuery(this).removeClass("ui-state-hover"); 
					}
				).mousedown(function(){
					jQuery(this).addClass("ui-state-active"); 
				})
				.mouseup(function(){
						jQuery(this).removeClass("ui-state-active");
				});
			//end button click

			// show
		    function doShow(){
				jQuery.post('${domainName}/show',{id:jQuery('#${domainName}_id').val()},function(json){
			    	 <%
                      props.each { p -> 
						%>
							jQuery('#${domainName}_${p.name}').val(json.${p.name});
						<%}%>
			     },'json');
					  
			};

			//List 
			jQuery("#${domainName}_list2").jqGrid({
				url: '${domainName}/jsonList',
				//width:1200,
				datatype:"json",
				colNames:[ 'id',
						<%
				           int count3=0
							 excludedProps = ['version',
							                  'id',
	                                            Events.ONLOAD_EVENT,
	                                            Events.BEFORE_DELETE_EVENT,
	                                            Events.BEFORE_INSERT_EVENT,
	                                            Events.BEFORE_UPDATE_EVENT]
		                         def props1 = domainClass.properties.findAll { !excludedProps.contains(it.name) }
								
		                         Collections.sort(props, comparator.constructors[0].newInstance([domainClass] as Object[]))
		                         props1.each { p -> 
		                         %>
		                         '${p.name}'<%if(count3<props1.size()-1){%>,<%}%>//width:auto
								<%count3++}%>],
				colModel:[
						{name:'id',index:'id', width:55, sortable:true,hidden:true},
						 <%excludedProps = ['version',
                                          'id',
                                            Events.ONLOAD_EVENT,
                                            Events.BEFORE_DELETE_EVENT,
                                            Events.BEFORE_INSERT_EVENT,
                                            Events.BEFORE_UPDATE_EVENT]
                         def props2 = domainClass.properties.findAll { !excludedProps.contains(it.name) }

                         Collections.sort(props, comparator.constructors[0].newInstance([domainClass] as Object[]))
						 int count2=0
                         props2.each { p ->
                         %>
                         {name:'${p.name}',index:'${p.name}', sortable:true}<%if(count2<props.size()-1){%>,<%}%>//width:auto
						<%count2++}%>
						],
				rowNum:10,
				rowList:[10,20,50],
				height:250,
				autowidth: true,
				rownumbers: true,
				imgpath: "\${resource(dir:'js/images', file:'')}",
				pager: jQuery('#${domainName}_pager2'), 
				sortorder: 'asc',
				sortname: 'name',
				viewrecords: true,
				autoencode: true,
				caption:"查询结果",
				onSelectRow:function(id){
								
							    var rd=jQuery('#${domainName}_list2').getRowData(id); 
								
								jQuery('#${domainName}_id').val(rd.id);
				}
				
				}).navGrid('#${domainName}_pager2',{edit:false,add:true,del:false});
			//end of list
			
			});

			var timeoutHnd;
			var flAuto = false;
			function doSearch(ev){
				if(!flAuto)
					return;
				// var elem = ev.target||ev.srcElement;
				if(timeoutHnd)
					clearTimeout(timeoutHnd);
				timeoutHnd = setTimeout(gridReload,500);
			}
			function gridReload(){
				var params=jQuery('#${domainName}_search').serialize();
				
				jQuery("#${domainName}_list2").setGridParam({url:"${domainName}/jsonList?"+params,page:1}).trigger("reloadGrid");
			}
			function enableAutosubmit(state){
				flAuto = state;
				jQuery("#${domainName}_button_search").attr("disabled",state);
			}
			function userDefineValidate(){
				//put user defined validate
			}


			//end!
							
	--></script>
<!--js area end -->	

